package com.scalar.forecast.projectSetup.actionCommands;

import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.commons.SessionKeys;
import com.scalar.forecast.projectSetup.MapPartNumbers;
import com.scalar.forecast.projectSetup.ReplacePartNumber;
import com.scalar.forecast.projectSetup.dao.ProjectSetupDAO;
import com.scalar.forecast.projectSetup.forms.MapPartNumbersForm;

/**
 * Processes the request for saving the Replaced Project Setup Part Numbers
 *
 * Created by Veeru.
 * Date: Oct 10, 2005
 * Time: 5:44:11 PM
 */

public class MapPartNumbersConfirmCommand extends ActionCommand {

    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response ) {

        ActionForward actionForward = mapping.findForward( "success" );
        MapPartNumbersForm mapPartNumbersForm = ( MapPartNumbersForm )form;
        MapPartNumbers mapPartNumbers = mapPartNumbersForm.getMapPartNumbers();

        boolean success = false;
        DBTransaction dbTransaction = null;
        HttpSession session = request.getSession ();

        HashMap errors = new HashMap ();
        request.setAttribute( SessionKeys.MESSAGES, errors );
        try {
            int projectId = mapPartNumbersForm.getProjectId();
            ArrayList chipsList  = mapPartNumbers.getChipsList();
            ArrayList boardsList = mapPartNumbers.getBoardsList();
            ArrayList kitsList   = mapPartNumbers.getKitsList();

            dbTransaction = DBTransaction.getInstance();
            dbTransaction.begin();
            for (int i = 0; i < chipsList.size(); i++) {
                ReplacePartNumber partNumber = (ReplacePartNumber) chipsList.get(i);
                ProjectSetupDAO.replaceProjectSetupPartNumber( dbTransaction, projectId, partNumber.getOriginalPartNumberId(), partNumber.getReplacingPartNumberId() );
            }
            for (int i = 0; i < boardsList.size(); i++) {
                ReplacePartNumber partNumber = (ReplacePartNumber) boardsList.get(i);
                ProjectSetupDAO.replaceProjectSetupPartNumber( dbTransaction, projectId, partNumber.getOriginalPartNumberId(), partNumber.getReplacingPartNumberId() );
            }
            for (int i = 0; i < kitsList.size(); i++) {
                ReplacePartNumber partNumber = (ReplacePartNumber) kitsList.get(i);
                ProjectSetupDAO.replaceProjectSetupPartNumber( dbTransaction, projectId, partNumber.getOriginalPartNumberId(), partNumber.getReplacingPartNumberId() );
            }
            success = true;
        }catch( Exception e ){
            e.printStackTrace();
        }
        finally {
            if ( success ) {
                try {
                    dbTransaction.commit();
                    errors.put( "Mapped Project Setup Part Numbers saved successfully", null);
                    actionForward = mapping.findForward( "confirmMapPartNumbers" );
                    request.setAttribute( "projectId", mapPartNumbersForm.getProjectId() + "" );
                    request.setAttribute( "CloseWindow", "CloseWindow" );
                }catch ( Exception e){
                    e.printStackTrace();
                }
            }else {
                try {
                    dbTransaction.rollback();
                } catch ( Exception e ){
                    e.printStackTrace();
                }
                errors.put( "Error : Could not save replaced Project Setup Part Numbers", null);
            }
        }
        return actionForward;
    }
}
